package com.edu.lagou.converter;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author meng.han
 * @date 2021-09-18
 */
public class DateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String s) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(s);
        } catch (ParseException e) {
            dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            try {
                return dateFormat.parse(s);
            } catch (ParseException parseException) {
                throw new IllegalArgumentException(e);
            }
        }
    }
}
